package com.somiz.admin;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import com.somiz.main.Control;
import com.somiz.objet.ObjetIdentifiableDaoImpl;

public class AdminDaoImpl extends ObjetIdentifiableDaoImpl<Admin> implements
		AdminDao {
	AdminDao adminDao;

	public Boolean isExiste(Admin admin, Session session) {
		Boolean isExiste = false;
		Admin adminTest = new Admin();
		List<Admin> admins = Control.getAdminDao().charger(adminTest, session);
		for (int i = 0; i < admins.size(); i++) {
			if (admin.getNom().equals(admins.get(i).getNom())) {
				if (admin.getMdp().equals(admins.get(i).getMdp())) {
					isExiste = true;
				}
			}
		}
		return isExiste;
	}

	public Boolean isExisteAvecNom(Admin admin, Session session) {
		Boolean isExiste = false;
		Admin adminTest = new Admin();
		List<Admin> admins = Control.getAdminDao().charger(adminTest, session);
		for (int i = 0; i < admins.size(); i++) {
			if (admin.getNom().equals(admins.get(i).getNom())) {
				isExiste = true;
			}
		}
		return isExiste;
	}

	public Boolean isRoot(Admin admin) {
		Boolean isRoot = false;
		if (admin.getNom().equals("root")) {
			if (admin.getMdp().equals("superroot")) {
				isRoot = true;
			}
		}
		return isRoot;
	}

	public Boolean isAdministrateur(Admin admin, Session session) {
		Boolean isAdministrateur = false;
		Admin adminTest = new Admin();
		List<Admin> admins = Control.getAdminDao().charger(adminTest, session);
		admin.setNiveau(Control.getAdminDao()
				.TrouverAvecNom(admin.getNom(), session).getNiveau());
		for (int i = 0; i < admins.size(); i++) {
			if (admin.getNom().equals(admins.get(i).getNom())) {
				if (admin.getNiveau().equals("Administrateur")) {
					isAdministrateur = true;
				}
			}
		}
		return isAdministrateur;
	}

	public List<String> ListeNomUser(Session session) {
		Admin adminTest = new Admin();
		List<Admin> admins = Control.getAdminDao().charger(adminTest, session);
		List<String> NomAdmins = new ArrayList<>(0);
		NomAdmins.add("");
		for (int i = 0; i < admins.size(); i++) {
			NomAdmins.add(admins.get(i).getNom());
		}
		return NomAdmins;
	}

	public Admin TrouverAvecNom(String nom, Session session) {
		Admin adminTest = new Admin();
		List<Admin> admins = Control.getAdminDao().charger(adminTest, session);
		Admin adminTrv = null;
		for (int i = 0; i < admins.size(); i++) {
			if (admins.get(i).getNom().equals(nom)) {
				adminTrv = admins.get(i);
				i = admins.size() + 1;
			}
		}
		return adminTrv;
	}

}
